<?php
/*
* Plugin Name: Menu Items
* Plugin URI: http://
* Description: Allows administrators/clients to dynamically update restaurant menus with custom post types. A key feature is the 
implementation of the already familiar WP user interface, to make things easy for the client to update the site.  
* Author: Bryan Russo
*/
//includes

// Example 2: admin_enqueue_scripts will run while browsing the back end

require_once ('inc/menu-items-template-tags.php');
					
add_action('admin_enqueue_scripts', 'jp_add_backend_js' );

function jp_add_backend_js( $hook_suffix ) {

        $src = plugins_url( 'js/script.js', __FILE__ );

        wp_enqueue_script( 'script', $src, array('jquery', 'jquery-ui-sortable') );

}

//On plugin activation
register_activation_hook( __FILE__, 'menu_items_on_activate');

//On plugin deactivation
register_deactivation_hook( __FILE__, 'menu_items_on_deactivate');

//wordpress hooks
add_action( 'add_meta_boxes', 'menu_items_add_metabox', 10, 2);
add_action( 'save_post', 'menu_items_save_post', 10, 2);


//callback function run on plugin activation
function menu_items_on_activate(){
	
	if ( version_compare( get_bloginfo( 'version' ). '3.0', '<' ) ) {
		deactivate_plugins(__FILE__);
	} else {
		
		flush_rewrite_rules();
		
		}
}

//callback function run on plugin deactivation
function menu_items_on_deactivate(){
	
		flush_rewrite_rules();
	
	}
	

//Create custom meta-boxes
	function menu_items_add_metabox( $post_type, $page ) {
	
		add_meta_box( 'menu-item-meta', //CSS of the <div> that wraps our metabox
					  'Dinner Items', //Title of the metabox
					  'menu_item_display_metabox', //Callback for rendering metabox content
					  'page', //Post type for meta box display
					  'normal', //Context  - part of page where meta will show
					  'high' //priority of metabox display related to other metaboxes on the page
					);
	
	}
	
	function menu_item_display_metabox( $page ) {
				
		$menu_input = get_post_meta( $page->ID, 'menu_input', true);
		
		if( $menu_input == false){
			
			$menu_input = array();
			
			$menu_input[1]['menu_item_name'] = 'Name of menu item';
			$menu_input[1]['menu_item_description'] = 'Description of menu item';
			$menu_input[1]['menu_item_price'] = "Menu item price";
			}	
						
		//display the form
		include 'interface/menu-items-metabox.php';
	}
		//run when a post is saved	
			function menu_items_save_post( $page_id, $page ){
				
				//bail on autosave
				if ($page->post_type == 'revision' ) {
					return;
			}
					
				if(isset ($_POST['menu_input']) ) {
					
					$menu_input = $_POST['menu_input'];
					
					/*echo "<pre>";
					
					print_r($menu_input);
					
					echo "</pre>";
					
					throw new Exception("Bam!");*/
					
					foreach ($menu_input as $index => $menu_item){
					
					$menu_input[$index]['menu_item_name'] = strip_tags( $menu_input[$index]['menu_item_name'] );
					$menu_input[$index]['menu_item_description'] = strip_tags ($menu_input[$index]['menu_item_description'] );
					$menu_input[$index]['menu_item_price'] = strip_tags( $menu_input[$index]['menu_item_price'] );
					
					}
										
					update_post_meta( $page_id, 'menu_input', $menu_input );
					
					}
					

				
				}
?>